﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace exsportance.Silverlight.Validate.Validators
{
    /// <summary>
    /// Validator for fields where a value matching against regular expression is needed
    /// </summary>
    public class RegexValidator : BaseValidator
    {
        #region members
        /// <summary>
        /// Validation failure message key for when the value is does not match
        /// </summary>
        private const string NOT_MATCH = "regexNotMatch";

        protected Dictionary<string, string> _messagesTemplates = new Dictionary<string, string>(){
        {RegexValidator.NOT_MATCH,"'%value%' does not match against pattern '%pattern%'"},
        };
        #endregion

        #region properties
        public string Pattern { get; set; }
        #endregion

        #region constructor
        public RegexValidator(string pattern)
        {
            Pattern = pattern;
        }
        #endregion

        #region protected methods
        protected override bool CheckValid(object value)
        {
            string valueString = value as string;

            rememberValue(valueString);

            if (!Regex.IsMatch(valueString, Pattern))
            {
                raiseMessage(RegexValidator.NOT_MATCH);
                return false;
            }

            return true;
        }

        protected override System.Collections.Generic.Dictionary<string, string> getMessageTemplates()
        {
            return _messagesTemplates;
        }
        #endregion
    }
}
